package com.auicyh.tylb.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 作者：  Ocean 2018/8/28 14:58
 * 信息：  Oil com.auicyh.tylb.adapter
 * 功能：  BaseCustomAdapter适配器的ViewHolder
 * 邮箱：   1126067113@qq.com
 * 修改人：
 * 公司: 北京天元陆兵汽车科技有限公司
 * @author Ocean
 */
public class ViewHolder {
    private static ViewHolder sViewHolder;

    /**
     * 私有的构造方法，避免这个类在外部被实例化
     */
    private ViewHolder() {
    }

    /**
     * 构造方法
     * @return ViewHolder 对象
     */
    public static ViewHolder getInstance() {
        if (sViewHolder == null) {
            sViewHolder = new ViewHolder();
        }
        return sViewHolder;
    }

    /**
     * 通过getTag获取控件view的标签，并强转成SparseArray对象,
     * 如果它为空这进行实例化，并把起设置成view的标签
     * @param convertView convertView
     * @param id          id
     * @param <T>         泛型
     * @return view
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T get(View convertView, int id) {
        SparseArray<View> sparseArray = (SparseArray<View>) convertView.getTag();
        if (sparseArray == null) {
            sparseArray = new SparseArray<>();
            convertView.setTag(sparseArray);
        }
        View childView = sparseArray.get(id);
        if (childView == null) {
            childView = convertView.findViewById(id);
            sparseArray.put(id, childView);
        }
        return (T) childView;
    }

    /**
     * 设置文字
     * @param convertView 布局View
     * @param childViewId 子控件id
     * @param txt         内容
     * @return ViewHolder
     */
    public ViewHolder setText(View convertView, int childViewId, String txt) {
        TextView textView = get(convertView, childViewId);
        textView.setText(txt);
        return this;
    }

    /**
     * 设置文字
     * @param convertView 布局View
     * @param childViewId 子控件id
     * @param txtRes      文字内容
     * @return ViewHolder
     */
    public ViewHolder setText(View convertView, int childViewId, @StringRes int txtRes) {
        TextView textView = get(convertView, childViewId);
        textView.setText(txtRes);
        return this;
    }

    /**
     * 设置文字图片
     * @param convertView   布局View
     * @param childViewId   子控件id
     * @param txt           文本
     * @param left          左图片
     * @param topDrawable   上图片
     * @param rightDrawable 右图片
     * @param bottom        下图片drawable对象
     * @return ViewHolder ViewHolder
     */
    public ViewHolder setImageText(View convertView, int childViewId, String txt, Drawable left,
                                   Drawable topDrawable, Drawable rightDrawable, Drawable bottom) {
        TextView view = get(convertView, childViewId);
        view.setText(txt);
        view.setCompoundDrawablesWithIntrinsicBounds(left, topDrawable, rightDrawable, bottom);
        return this;
    }

    /**
     * 设置文字左图片
     * @param convertView 布局View
     * @param childViewId 子控件id
     * @param txt         文本
     * @param left        左图片
     * @return ViewHolder
     */
    public ViewHolder setLeftImageText(View convertView, int childViewId, String txt, Drawable left) {
        TextView view = get(convertView, childViewId);
        view.setText(txt);
        view.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
        return this;
    }

    /**
     * 设置文字左图片
     * @param convertView 布局View
     * @param childViewId 子控件id
     * @param txt         文本
     * @param top         上图片
     * @return ViewHolder
     */
    public ViewHolder setTopImageText(View convertView, int childViewId, String txt, Drawable top) {
        TextView view = get(convertView, childViewId);
        view.setText(txt);
        view.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
        return this;
    }

    /**
     * 设置文字右图片
     * @param convertView 布局View
     * @param childViewId 子控件id
     * @param txt         文本
     * @param right       右图片
     * @return ViewHolder
     */
    public ViewHolder setRightImageText(View convertView, int childViewId, String txt, Drawable right) {
        TextView view = get(convertView, childViewId);
        view.setText(txt);
        view.setCompoundDrawablesWithIntrinsicBounds(null, null, right, null);
        return this;
    }

    /**
     * 设置文字下图片
     * @param convertView 布局View
     * @param childViewId 子控件id
     * @param txt         文本
     * @param bottom      下图片
     * @return ViewHolder
     */
    public ViewHolder setBottomImageText(View convertView, int childViewId, String txt, Drawable bottom) {
        TextView view = get(convertView, childViewId);
        view.setText(txt);
        view.setCompoundDrawablesWithIntrinsicBounds(null, null, null, bottom);
        return this;
    }


    /**
     * 设置图片
     * @param convertView 布局View
     * @param childViewId 子控件id
     * @param drawable    drawable
     * @return ViewHolder
     */
    public ViewHolder setImage(View convertView, int childViewId, Drawable drawable) {
        ImageView imageView = get(convertView, childViewId);
        imageView.setImageDrawable(drawable);
        return this;
    }
}
